*******************************************************************************
* Load data
*******************************************************************************

use dataset_polls, clear

*******************************************************************************
* Keep only polls where reasons asked
*******************************************************************************

drop if mi(fraudc) & mi(fraudflc) & mi(electoralcollegec) & mi(hackingc)

*******************************************************************************
* If both vote choice and party included, take the average 
*******************************************************************************

replace subsample = "Dem" if regex(lower(subsample), "dem") 
replace subsample = "Rep" if regex(lower(subsample), "rep")
keep if inlist(subsample, "Dem", "Rep")

collapse legitimatec fraudc fraudflc electoralcollegec hackingc, ///
	by(subsample date house2)

*******************************************************************************
* Reshape to date-house-subsample level
*******************************************************************************

gen datehouse = string(date, "%d") + "-" + house2
reshape i datehouse
reshape j subsample, string
reshape xi date house2
reshape xij legitimatec fraudc fraudflc electoralcollegec hackingc
reshape wide		

*******************************************************************************
* > Loop over reasons
*******************************************************************************

foreach reason in "Fraud" "ElectoralCollege" "Hacking" {

	preserve
		
		local myreason = lower("`reason'")
		
		drop if mi(`myreason'cDem) & mi(`myreason'cRep)
		
		#delimit;
		local myscatters
			(sc `myreason'cDem date, msym(Oh) col(black))
			(sc `myreason'cRep date, msym(O) col(black))
			;
		#delimit cr
		
		local mytext1
		local mytext2

		if "`reason'" == "Fraud" {

			#delimit;
			local myscatters	
				(sc fraudcDem date, msym(Oh) col(black))
				(sc fraudflcDem date, msym(th) col(black))
				(sc fraudcRep date, msym(O) col(black))
				(sc fraudflcRep date, msym(t) col(black))
				;
			#delimit cr
			
			local mytext1 text(0.4 15706 "Dem", col(black))
			local mytext2 text(0.1 15706 "Rep", col(black))
		
		}

*******************************************************************************
* Plot
*******************************************************************************

		format date %d

		#delimit;

		gr tw	
			`myscatters'
			,
				ylab(, angle(horiz) labcol(black))
				xlab(, labsize(small) labcol(black))
				legend(off)
				plotregion(style(none))
				ytitle("")
				xtitle("")
				title(`reason', col(black))
				name(g`reason', replace)
				`mytext1'
				`mytext2'
				xscale(lcol(black))
				yscale(lcol(black))
				;
				
		#delimit cr
		
	restore

*******************************************************************************
* > Close loop over reasons
*******************************************************************************

}

*******************************************************************************
* Combine plots
*******************************************************************************

gr combine gFraud gElectoralCollege gHacking, rows(3) ///
	xsize(3) ysize(3.5) ycommon 
	
*******************************************************************************
* Save
*******************************************************************************

gr export "_output/figure_appendix_reasons.pdf", replace

*******************************************************************************
* End
*******************************************************************************
